package com.itcoon.cloud.framework.transform.assemble.assembler.inter;

import com.itcoon.cloud.framework.transform.definition.InjectDefinition;
import com.itcoon.cloud.framework.transform.exception.AssembleException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class MethodAssemblerComposite {

    private static final Logger log = LoggerFactory.getLogger(MethodAssemblerComposite.class);

    private List<MethodAssembler> methodAssemblers;

    public <T, R> Object assemble(T source, Class<R> targetClass, Object proxy, Method method, String[] ignoreProperties, List<InjectDefinition> injectInfos, Object[] args) {
        for (MethodAssembler methodAssembler : methodAssemblers) {
            if (methodAssembler.support(targetClass, source, method, ignoreProperties)) {
                try {
                    return methodAssembler.assemble(source, targetClass, proxy, method, ignoreProperties, injectInfos, args);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    log.error("[Transform4j] - assemble method named #{} failure, it required args is {}, the reason is {}.",method.getName(), Arrays.toString(args), e.getMessage());
                    throw new AssembleException(e.getMessage());
                }
            }
        }
        return null;
    }
}
